﻿package cn.poco.adapter;

import java.util.List;

import cn.poco.bean.Restaurant;
import cn.poco.constants.Constants;
import cn.poco.xw.R;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SearchResAdapter extends BaseAdapter {
	
	//private final static String TAG = "SearchResAdapter";
	private List<Restaurant> restaurant;
	@SuppressWarnings("unused")
	private Context context;
	private LayoutInflater myinflater;
	
	private Boolean lastColumn;
	private Boolean rankNum = false;	//是否显示序号

	public SearchResAdapter(Context context,List<Restaurant> restaurant,Handler handler,
			ListView listview){
		this.context = context;
		this.restaurant = restaurant;
		this.myinflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return restaurant.size();
	}

	@Override
	public Object getItem(int position) {
		return restaurant.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.parseLong(restaurant.get(position).getId().toString());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderItem holder = null;
		if(convertView==null){
			holder  = new ViewHolderItem();
			convertView = myinflater.inflate(R.layout.main_search_res_item, null);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.image = (ImageView) convertView.findViewById(R.id.star);
			holder.average = (TextView) convertView.findViewById(R.id.average);
			holder.dish = (TextView) convertView.findViewById(R.id.dish);
			holder.circle = (TextView) convertView.findViewById(R.id.circle);
			holder.join =(ImageView) convertView.findViewById(R.id.join);
			holder.pepsi = (ImageView)convertView.findViewById(R.id.pepsi);
			holder.discount = (ImageView)convertView.findViewById(R.id.discount);
			holder.distance = (TextView)convertView.findViewById(R.id.distance);
			holder.num = (TextView)convertView.findViewById(R.id.num);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolderItem)convertView.getTag();
		}
		
		Restaurant res =restaurant.get(position);
			
			if(getRankNum()){
				int size = position +1;
				holder.num.setText(size+"");
				holder.num.setVisibility(View.VISIBLE);
			}
				holder.title.setText(res.getTitle());
			
			
			String average = res.getAverage();
			if(average!=null && average!=""){
				holder.average.setText("人均："+res.getAverage());
			}
			holder.dish.setText(res.getDish());
			holder.circle.setText(res.getArea());
			String distancestr = res.getDistance();
			if(distancestr!=null && !distancestr.equals("") && !distancestr.equals("0") ){
				holder.distance.setText(distancestr+"米");
				holder.distance.setVisibility(View.VISIBLE);
			}
			String startNum = res.getStar();
			int start = -1;
			if(startNum !=null && !startNum.equals("")){
				start = Integer.parseInt(res.getStar());
			}
			//设置餐厅星级
			switch (start) {
			case Constants.ONE_STAR:
				holder.image.setImageResource(R.drawable.x1); break;
			case Constants.TWO_STAR:
				holder.image.setImageResource(R.drawable.x2); break;
			case Constants.THREE_STAR:
				holder.image.setImageResource(R.drawable.x3); break;
			case Constants.FOUR_STAR:
				holder.image.setImageResource(R.drawable.x4); break;
			case Constants.FIVE_STAR:
				holder.image.setImageResource(R.drawable.x5); break;
			default:
				holder.image.setImageResource(R.drawable.x0); break;
			}
			//优惠加盟信息设置
			if(res.getPepsi()==1){
				holder.pepsi.setVisibility(View.VISIBLE);
			}
			if(res.getDiscount()==1){
				holder.discount.setVisibility(View.VISIBLE);
			}
			if(res.getVouch()==1){
				holder.join.setVisibility(View.VISIBLE);
			}
			return convertView;

	}
	public final class ViewHolderItem{
		 public TextView title;
		 public ImageView image;
		 public TextView average;
		 public TextView dish;
		 public TextView circle;
		 public ImageView join;
		 public ImageView pepsi;
		 public ImageView discount;
		 private TextView distance;
		 public LinearLayout layout;
		 public TextView num;
		}
	public Boolean getLastColumn() {
		return lastColumn;
	}

	public void setLastColumn(Boolean lastColumn) {
		this.lastColumn = lastColumn;
	}

	public Boolean getRankNum() {
		return rankNum;
	}

	public void setRankNum(Boolean rankNum) {
		this.rankNum = rankNum;
	}

}
